package org.tuoren.official.controller;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.tuoren.common.idempotent.annotation.RepeatSubmit;
import org.tuoren.common.log.annotation.Log;
import org.tuoren.common.web.core.BaseController;
import org.tuoren.common.mybatis.core.page.PageQuery;
import org.tuoren.common.core.domain.R;
import org.tuoren.common.core.validate.AddGroup;
import org.tuoren.common.core.validate.EditGroup;
import org.tuoren.common.log.enums.BusinessType;
import org.tuoren.common.excel.utils.ExcelUtil;
import org.tuoren.official.domain.vo.HistoryVo;
import org.tuoren.official.domain.bo.HistoryBo;
import org.tuoren.official.service.IHistoryService;
import org.tuoren.common.mybatis.core.page.TableDataInfo;

/**
 * 发展历程
 *
 * @author wl
 * @date 2024-11-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/official/history")
public class HistoryController extends BaseController {

    private final IHistoryService historyService;


     /**
     * 前台-查询发展历程列表
     */
    @SaIgnore
    @GetMapping("/frontList")
    public TableDataInfo<HistoryVo> frontList() {
        return historyService.frontList();
    }

    /**
     * 查询发展历程列表
     */
    @SaCheckPermission("official:history:list")
    @GetMapping("/list")
    public TableDataInfo<HistoryVo> list(HistoryBo bo, PageQuery pageQuery) {
        return historyService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出发展历程列表
     */
    @SaCheckPermission("official:history:export")
    @Log(title = "发展历程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HistoryBo bo, HttpServletResponse response) {
        List<HistoryVo> list = historyService.queryList(bo);
        ExcelUtil.exportExcel(list, "发展历程", HistoryVo.class, response);
    }

    /**
     * 获取发展历程详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("official:history:query")
    @GetMapping("/{id}")
    public R<HistoryVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(historyService.queryById(id));
    }

    /**
     * 新增发展历程
     */
    @SaCheckPermission("official:history:add")
    @Log(title = "发展历程", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HistoryBo bo) {
        return toAjax(historyService.insertByBo(bo));
    }

    /**
     * 修改发展历程
     */
    @SaCheckPermission("official:history:edit")
    @Log(title = "发展历程", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/edit")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HistoryBo bo) {
        return toAjax(historyService.updateByBo(bo));
    }

    /**
     * 删除发展历程
     *
     * @param ids 主键串
     */
    @SaCheckPermission("official:history:remove")
    @Log(title = "发展历程", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(historyService.deleteWithValidByIds(List.of(ids), true));
    }
}
